<?php
error_reporting(E_ERROR);
ini_set("display_errors", "Off");


function request_post($url = '', $param = '')
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}

require './FileUpload.php';
$file=isset($_FILES['photo'])?$_FILES['photo']:"";
$fileUpload=new FileUpload();
$allPath=$fileUpload->upload($file);
if(!$allPath){
    //上传失败,打印错误信息
    echo $fileUpload->errorMessage;
    //结束运行
    die();
}
$token = '填写自己的口令';
$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=' . $token;
$img = file_get_contents($allPath);
$img = base64_encode($img);
$bodys = array(
    'image' => $img
);
$res = request_post($url, $bodys);
// var_dump($res);
//json转成数组格式
$result = json_decode($res,true);


/* foreach($result as $value){
	echo "$value";
} */
foreach($result as $value){
	foreach($value as $value1){
		foreach ($value1 as $key=>$value2){
			echo "[$key] = $value2 <br />";
		}
	}
}

// var_dump($result);

// echo $res;
require './index.html';